﻿#region Copyright and license information
// Copyright 2012 Richard Schneider
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

using System;
using NUnit.Framework;
using NodaTime.Text;

namespace NodaTime.Test.Text
{

    [TestFixture]
    public class PeriodNormalizingXmlPattern
    {
        static PeriodPattern pattern = PeriodPattern.NormalizingXmlPattern;

        [Test]
        public void Zero()
        {
            Assert.AreEqual("P0D", pattern.Format(Period.Zero));
        }

        [Test]
        public void NegativeComponents()
        {
            var result = pattern.Parse("-P1DT2H");
            Assert.IsTrue(result.Success);
            Assert.AreEqual(-1, result.Value.Days);
            Assert.AreEqual(-2, result.Value.Hours);
            Assert.AreEqual("-P1DT2H", pattern.Format(result.Value));

            Assert.AreEqual("-P1Y2M25DT5H6M7.0080009S", pattern.Format(new Period(-1, -2, -3, -4, -5, -6, -7, -8, -9)));
            Assert.AreEqual(new Period(-1, -2, 0, -25, -5, -6, -7, -8, -9), pattern.Parse("-P1Y2M25DT5H6M7.0080009S").Value );
        }

        [Test]
        public void PositiveAndNegativeComponents()
        {
            var result = pattern.Parse("P1DT-2H");
            Assert.IsFalse(result.Success);
            Assert.Throws<ArgumentException>(() => pattern.Format(new Period(0, 0, 0, 1, -2, 0, 0, 0, 0)));
        }

        [Test]
        public void NormalizedWeeks()
        {
            Assert.AreEqual("P8D", pattern.Format(new Period(0, 0, 1, 1, 0, 0, 0, 0, 0)));
            Assert.AreEqual("-P15D", pattern.Format(new Period(0, 0, -2, -1, 0, 0, 0, 0, 0)));
        }

        [Test]
        public void FractionalSeconds()
        {
            var result = pattern.Parse("PT0.1234567S");
            Assert.IsTrue(result.Success);
            Assert.AreEqual(0, result.Value.Seconds);
            Assert.AreEqual(123, result.Value.Milliseconds);
            Assert.AreEqual(4567, result.Value.Ticks);

            result = pattern.Parse("-PT0.1234567S");
            Assert.IsTrue(result.Success);
            Assert.AreEqual(0, result.Value.Seconds);
            Assert.AreEqual(-123, result.Value.Milliseconds);
            Assert.AreEqual(-4567, result.Value.Ticks);

            Assert.AreEqual("PT0.001S", pattern.Format(new Period(0, 0, 0, 0, 0, 0, 0, 1, 0)));
            Assert.AreEqual("PT0.0000001S", pattern.Format(new Period(0, 0, 0, 0, 0, 0, 0, 0, 1)));
            Assert.AreEqual("-PT0.001S", pattern.Format(new Period(0, 0, 0, 0, 0, 0, 0, -1, 0)));
            Assert.AreEqual("-PT0.0000001S", pattern.Format(new Period(0, 0, 0, 0, 0, 0, 0, 0, -1)));
            Assert.AreEqual("-PT1.0020003S", pattern.Format(new Period(0, 0, 0, 0, 0, 0, -1, -2, -3)));
        }
    }
}
